package com.douqu.game.core.config.card;

import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: Gavin.
 * Description:
 * Date: 2017/11/1 0001 下午 7:11
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class CardStarConfig extends GameObject{

    /**需要的资源*/
//    public GoodsData [] needAssets;
    public Map<Integer, CommonData> needAssets;
    /**需要的兵种碎片数量
     * 这里的ID是不固定的，根据不同的卡牌检测不同的碎片
     * */
    public GoodsData [] needDebris;
    /**需要的突破令数量
     * 这里的ID是不固定的，根据不同的卡牌检测不同的碎片
     * */
    public Map<Integer, CommonData> needProps;
    /**返还的资源*/
    public GoodsData [] restitutionResource;
    /**返还的道具*/
    public GoodsData [] restitutionItem;
    /**重生返回的碎片数量**/
    public GoodsData [] backDebris;

    /**解锁等级*/
    public int unlockLevel;
    /**
     * 直接增加的战斗力
     */
    public CommonData[] addFC;

    public String restitutionOrange;
    public String restitutionRed;

//    /**
//     * 可以升此星的品质
//     */
//    public int[] quality;

    public CardStarConfig() {
        needAssets = new ConcurrentHashMap<>();
        needProps = new ConcurrentHashMap<>();
    }
//
//    public List<Integer> qualityList(){
//        if (quality == null || quality.length == 0){
//            return new ArrayList<>();
//        }
//        List<Integer> qualityList = new ArrayList<>();
//        for (int i:quality){
//            qualityList.add(i);
//        }
//        return qualityList;
//    }

    @Override
    public boolean check()
    {
        if(needAssets == null)
        {
            LogUtils.error("CardStar check needAssets is null -> id:" + id + " name:" + name);
            return false;
        }
        if(needDebris == null)
        {
            LogUtils.error("CardStar check needDebris is null -> id:" + id + " name:" + name);
            return false;
        }
        if(backDebris == null)
        {
            LogUtils.error("CardStar check backDebris is null -> id:" + id + " name:" + name);
            return false;
        }
        if(needProps == null)
        {
            LogUtils.error("CardStar check needProps is null -> id:" + id + " name:" + name);
            return false;
        }
        if(restitutionResource == null)
        {
            LogUtils.error("CardStar check restitutionResource is null -> id:" + id + " name:" + name);
            return false;
        }

        if(restitutionItem == null)
        {
            LogUtils.error("CardStar check restitutionItem is null -> id:" + id + " name:" + name);
            return false;
        }

//        if(quality == null){
//            LogUtils.error("CardStar ceheck quality is null -> :" + id + " name:" + name);
//            return false;
//        }
        return true;
    }


    public TwoTuple<Boolean, Integer> checkAsset(BagInfo bagInfo, int quality){

        CommonData need = needAssets.get(quality);
        if(need == null)
            return new TwoTuple<>(true, 0);

        if(bagInfo.getAsset(need.id) < need.value)
        {
            return new TwoTuple<>(false, need.id);
        }
        else
            return new TwoTuple<>(true, 0);
    }


    public TwoTuple<Boolean, Integer> checkProps(BagInfo bagInfo ,int quality){
        CommonData need  = needProps.get(quality);
        if(need == null)
            return new TwoTuple<>(true, 0);

        if(bagInfo.getPropCount(need.id) < need.value)
        {
            return new TwoTuple<>(false, need.id);
        }
        else
            return new TwoTuple<>(true, 0);
    }

    public boolean checkDebris(BagInfo bagInfo, int quality, int propId){
        for (int i =0 ; i  < needDebris.length; i++){
            //品质
            if(quality == needDebris[i].type){
                return bagInfo.getPropCount(propId) >= needDebris[i].value;
            }
        }
        return false;
    }


    /**
     * 获取星级的额外战力加成
     * @param quality
     * @return
     */
    public int getAddFC(int quality)
    {
        for(CommonData commonData : addFC)
        {
            if(quality == commonData.id)
                return commonData.value;
        }

        return 0;
    }

    @Override
    public boolean setVariable(String key, String value)
    {
        if("needAssets".equals(key)){
            String[] strs = value.split(ConstantFactory.DIVISION);
            GoodsData temp = null;
            for(int i = 0; i < strs.length; i++)
            {
                temp = new GoodsData(strs[i]);
                needAssets.put(temp.type, new CommonData(temp.id, temp.value));

                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.ASSET_KEY, temp.id) == null)
                {
                    LogUtils.error("CardStar setVariable needAssets Error -> type : " + temp.type + " id:" + temp.id + " num:" + temp.value);
                    return false;
                }
            }
            return true;
        }
        else if("needDebris".equals(key))
        {
            String[] strs = value.split(ConstantFactory.DIVISION);
            needDebris = new GoodsData[strs.length];
            String[] temp = null;
            for(int i = 0; i < strs.length; i++)
            {
                temp = strs[i].split(ConstantFactory.SEMICOLON);
                needDebris[i] = new GoodsData(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]));
            }
            return true;
        }
        else if("backDebris".equals(key))
        {
            String[] strs = value.split(ConstantFactory.DIVISION);
            backDebris = new GoodsData[strs.length];
            String[] temp = null;
            for(int i = 0; i < strs.length; i++)
            {
                temp = strs[i].split(ConstantFactory.SEMICOLON);
                backDebris[i] = new GoodsData(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]));
            }
            return true;
        }
        else if("needProps".equals(key))
        {
            String[] strs = value.split(ConstantFactory.DIVISION);
            GoodsData temp = null;
            for(int i = 0; i < strs.length; i++)
            {
                temp = new GoodsData(strs[i]);
                needProps.put(temp.type, new CommonData(temp.id, temp.value));
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.PROP_KEY, temp.id) == null)
                {
                    LogUtils.error("CardStar setVariable needProps Error -> type : " + temp.type + " id:" + temp.id + " num:" + temp.value);
                    return false;
                }
            }
            return true;
        }
        else if("restitutionResource".equals(key))
        {
            restitutionResource = LoadUtils.loadGoodDataToArray(key, value);
            GoodsData[] checkData = new GoodsData[restitutionResource.length];
            for(int i = 0; i < restitutionResource.length; i ++){
                checkData[i] = new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE, restitutionResource[i].id, restitutionResource[i].value);
            }
            return LoadUtils.checkGoods(checkData, this.getClass(), key, id);
        }
        else if("restitutionItem".equals(key))
        {
            restitutionItem = LoadUtils.loadGoodDataToArray(key, value);
            GoodsData[] checkData = new GoodsData[restitutionItem.length];
            for(int i = 0; i < restitutionItem.length; i ++){
                checkData[i] = new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE, restitutionItem[i].id, restitutionItem[i].value);
            }
            return LoadUtils.checkGoods(checkData, this.getClass(), key, id);
        }
        else if("addFC".equals(key))
        {
            addFC = LoadUtils.loadCommonDataToArray(key, value);
            return true;
        }
        else{
            return super.setVariable(key, value);
        }
    }


}
